<?php
	class xl_sanpham extends database
	{
		function LoadAll()
		{
			$sql = "Select * from sanpham";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}

		function Load_SanPham_TheoMa($masanpham)
		{
			$sql = "Select * from sanpham where masanpham= '".$masanpham."' ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadOneRow();
			$this->disconnect();
			return $arr;
		}



		function Load_SanPham_TheoThuongHieu($mathuonghieu)
		{
			$sql = "Select * from sanpham where mathuonghieu= '".$mathuonghieu."' ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}

		function Load_SanPham_TheoLoaiSanPham($maloaisanpham)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '".$maloaisanpham."'
				ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}

		function Load_SanPham_TheoLoaiSanPham_Limit($maloaisanpham, $start, $display)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '".$maloaisanpham."'
				ORDER BY (ngaydang) DESC
			    LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_SanPham_TheoLoaiSanPham_CongSuat($maloaisanpham, $macongsuat)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '$maloaisanpham'
				AND s.macongsuat = '$macongsuat'
				ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_SanPham_TheoLoaiSanPham_CongSuat_Limit($maloaisanpham, $macongsuat, $start, $display)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '".$maloaisanpham."'
				AND s.macongsuat = '".$macongsuat."'
				ORDER BY (ngaydang) DESC
				LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_SanPham_TheoLoaiSanPham_CongSuat_ThuongHieu($maloaisanpham, $macongsuat, $mathuonghieu)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '$maloaisanpham'
				AND s.macongsuat = '$macongsuat'
				AND s.mathuonghieu = '$mathuonghieu'
				ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_SanPham_TheoLoaiSanPham_CongSuat_ThuongHieu_Limit($maloaisanpham, $macongsuat, $mathuonghieu, $start, $display)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '$maloaisanpham'
				AND s.macongsuat = '$macongsuat'
				AND s.mathuonghieu = '$mathuonghieu'
				ORDER BY (ngaydang) DESC
				LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_ThuongHieu_Co_SanPham()
		{
			$sql = "SELECT  t.mathuonghieu, t.tenthuonghieu, t.hinhanh
					FROM sanpham s, thuonghieu t
					WHERE s.mathuonghieu = t.mathuonghieu
					GROUP BY t.mathuonghieu";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		//lấy tất cả thương hiệu của từng loại sản phẩm
		function Load_ThuongHieu_TheoLoaiSanPham($maloaisanpham)
		{
			$sql = "SELECT  t.mathuonghieu, t.tenthuonghieu, t.hinhanh, COUNT( s.masanpham ) AS soluong
					FROM sanpham s, thuonghieu t, loaisanpham l
					WHERE s.mathuonghieu = t.mathuonghieu
					AND s.maloaisanpham = l.maloaisanpham
					AND l.maloaisanpham = '".$maloaisanpham."'
					GROUP BY t.mathuonghieu";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_ThuongHieu_TheoLoaiSanPham_CongSuat($maloaisanpham,$macongsuat)
		{
			$sql = "SELECT  t.mathuonghieu, t.tenthuonghieu, t.hinhanh, COUNT( s.masanpham ) AS soluong
					FROM sanpham s, thuonghieu t, loaisanpham l
					WHERE s.mathuonghieu = t.mathuonghieu
					AND s.maloaisanpham = l.maloaisanpham
					AND l.maloaisanpham = '$maloaisanpham'
					AND s.macongsuat = '$macongsuat'
					GROUP BY t.mathuonghieu";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_CongSuat_TheoLoaiSanPham_ThuongHieu($maloaisanpham, $mathuonghieu)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '".$maloaisanpham."'
				AND t.mathuonghieu = '".$mathuonghieu."'
				GROUP BY s.macongsuat
				ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_SanPham_TheoLoaiSanPham_ThuongHieu($maloaisanpham, $mathuonghieu)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '".$maloaisanpham."'
				AND t.mathuonghieu = '".$mathuonghieu."'
				ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}

		function Load_SanPham_TheoLoaiSanPham_ThuongHieu_Limit($maloaisanpham, $mathuonghieu, $start, $display)
		{
			$sql = "SELECT * FROM sanpham s, thuonghieu t, congsuat c, loaisanpham l
				WHERE s.mathuonghieu= t.mathuonghieu
				AND s.macongsuat = c.macongsuat
				AND s.maloaisanpham = l.maloaisanpham
				AND l.maloaisanpham = '".$maloaisanpham."'
				AND t.mathuonghieu = '".$mathuonghieu."'
				ORDER BY (ngaydang) DESC
				LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}

		
		function Load_SanPham_NoiBat($macongsuat)
		{
			$sql = "Select * from sanpham where macongsuat= '".$macongsuat."' ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}


		function Them_SanPham($maloaisanpham, $mathuonghieu, $macongsuat, $tensanpham, $gia, $ngaydang, $motangangon, $motasanpham, $thongsokythuat, $sanphamnoibat, $hinhanh1, $tags, $mota)
		{
			$sql = "INSERT INTO sanpham(maloaisanpham, mathuonghieu, macongsuat, tensanpham, gia, ngaydang, motangangon, motasanpham, thongsokythuat, sanphamnoibat, hinhanh1, tags, mota) VALUES ('".$maloaisanpham."','". $mathuonghieu."', '".$macongsuat."', '".$tensanpham."', '".$gia."', '".$ngaydang."', '".$motangangon."', '".$motasanpham."', '".$thongsokythuat."', '".$sanphamnoibat."', '".$hinhanh1."', '".$tags."', '".$mota."')";
			$this->setQuery($sql);
			return $this->query();
		}

		function Cap_Nhat_SanPham($masanpham, $maloaisanpham, $mathuonghieu, $macongsuat, $tensanpham, $gia,  $motangangon, $motasanpham, $thongsokythuat, $sanphamnoibat, $tags, $mota)
		{
			$sql = "UPDATE sanpham SET maloaisanpham='".$maloaisanpham."',mathuonghieu='". $mathuonghieu."',macongsuat='".$macongsuat."',tensanpham='".$tensanpham."',gia='".$gia."',motangangon='".$motangangon."',motasanpham='".$motasanpham."',thongsokythuat='".$thongsokythuat."',sanphamnoibat='".$sanphamnoibat."',tags='".$tags."',mota='".$mota."' where masanpham= '".$masanpham."'";
			$this->setQuery($sql);
			return $this->query();
		}

		function Them_hinh_anh($masanpham, $hinhanh, $index)
		{
			$sql = "UPDATE sanpham SET hinhanh".$index."='".$hinhanh."' where masanpham= '".$masanpham."'";
			$this->setQuery($sql);
			return $this->query();
		}

		function Xoa_hinh_anh($masanpham, $index)
		{
			$sql = "UPDATE sanpham SET hinhanh".$index."='' where masanpham= '".$masanpham."'";
			$this->setQuery($sql);
			return $this->query();
		}

		function Xoa_SanPham($masanpham)
		{
			$sql = "DELETE FROM sanpham where masanpham= '".$masanpham."'";
			$this->setQuery($sql);
			return $this->query();
		}
		
		function Load_TenLoaiSanPham_TenCongSuat($MaLoaiSanPham)
		{
			$sql ="SELECT c.macongsuat, c.tencongsuat, l.maloaisanpham, l.tenloaisanpham
					FROM sanpham s, congsuat c, loaisanpham l
					WHERE s.macongsuat = c.macongsuat
					AND s.maloaisanpham = l.maloaisanpham
					AND s.maloaisanpham = $MaLoaiSanPham
					GROUP BY s.macongsuat
					ORDER BY (
					tencongsuat
					)";	
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;			
		}
		
		function Load_SP_Theo_MaLoaiSanPham_MaCongSuat($MaLoaiSanPham, $MaCongSuat)
		{
			$sql ="SELECT *
					FROM sanpham
					WHERE maloaisanpham = $MaLoaiSanPham
					AND macongsuat = $MaCongSuat					
					ORDER BY (
					ngaydang
					) DESC";	
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;			
		}
		
		function Load_SP_NoiBat()
		{
			$sql = "Select * from sanpham where sanphamnoibat= '1' ORDER BY (ngaydang) DESC LIMIT 0, 6";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;
		}
		
		function Load_SP_Cung_ThuongHieu($mathuonghieu, $masanpham)
		{
			$sql = "Select * from sanpham where mathuonghieu= '$mathuonghieu'
						and masanpham <> $masanpham
						ORDER BY (ngaydang) DESC LIMIT 0, 6";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Load_SP_Moi_Top6()
		{
			$sql = "Select * from sanpham ORDER BY (ngaydang) DESC LIMIT 0, 6";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_LoaiSP($MaLoaiSanPham)
		{
			$sql = "Select * from sanpham where maloaisanpham= '$MaLoaiSanPham'
						ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_LoaiSP_Limit($MaLoaiSanPham, $start, $display)
		{
			$sql = "Select * from sanpham where maloaisanpham= '$MaLoaiSanPham'
						ORDER BY (ngaydang) DESC
						LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_CongSuat($MaCongSuat)
		{
			$sql = "Select * from sanpham where macongsuat= '$MaCongSuat'
						ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_CongSuat_Limit($MaCongSuat, $start, $display)
		{
			$sql = "Select * from sanpham where macongsuat= '$MaCongSuat'
						ORDER BY (ngaydang) DESC
						LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_Gia($GiaMin, $GiaMax)
		{
			$sql = "SELECT * 
					FROM sanpham
					WHERE gia
					BETWEEN  '$GiaMin'
					AND  '$GiaMax'
					ORDER BY (
					ngaydang
					) DESC ";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_Gia_Limit($GiaMin, $GiaMax, $start, $display)
		{
			$sql = "SELECT * 
					FROM sanpham
					WHERE gia
					BETWEEN  '$GiaMin'
					AND  '$GiaMax'
					ORDER BY (
					ngaydang
					) DESC 
					LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_LoaiSP_CongSuat($MaLoaiSanPham, $MaCongSuat)
		{
			$sql = "Select * from sanpham where maloaisanpham= '$MaLoaiSanPham'
					and macongsuat = '$MaCongSuat'
						ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_LoaiSP_CongSuat_Limit($MaLoaiSanPham, $MaCongSuat, $start, $display)
		{
			$sql = "Select * from sanpham where maloaisanpham= '$MaLoaiSanPham'
					and macongsuat = '$MaCongSuat'
						ORDER BY (ngaydang) DESC
						LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_LoaiSP_Gia($MaLoaiSanPham, $GiaMin, $GiaMax)
		{
			$sql = "Select * from sanpham where maloaisanpham= '$MaLoaiSanPham'
					and gia
					BETWEEN  '$GiaMin'
					AND  '$GiaMax'
						ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_LoaiSP_Gia_Limit($MaLoaiSanPham, $GiaMin, $GiaMax, $start, $display)
		{
			$sql = "Select * from sanpham where maloaisanpham= '$MaLoaiSanPham'
					and gia
					BETWEEN  '$GiaMin'
					AND  '$GiaMax'
						ORDER BY (ngaydang) DESC
						LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_CongSuat_Gia($MaCongSuat, $GiaMin, $GiaMax)
		{
			$sql = "Select * from sanpham where macongsuat= '$MaCongSuat'
					and gia
					BETWEEN  '$GiaMin'
					AND  '$GiaMax'
						ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_CongSuat_Gia_Limit($MaCongSuat, $GiaMin, $GiaMax, $start, $display)
		{
			$sql = "Select * from sanpham where macongsuat= '$MaCongSuat'
					and gia
					BETWEEN  '$GiaMin'
					AND  '$GiaMax'
						ORDER BY (ngaydang) DESC
						LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_LoaiSp_CongSuat_Gia($MaLoaiSP, $MaCongSuat, $GiaMin, $GiaMax)
		{
			$sql = "Select * from sanpham where
					maloaisanpham = '$MaLoaiSP'
					and macongsuat= '$MaCongSuat'
					and gia
					BETWEEN  '$GiaMin'
					AND  '$GiaMax'
						ORDER BY (ngaydang) DESC";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
		
		function Tim_Kiem_SP_Theo_LoaiSp_CongSuat_Gia_Limit($MaLoaiSP, $MaCongSuat, $GiaMin, $GiaMax, $start, $display)
		{
			$sql = "Select * from sanpham where maloaisanpham = '$MaLoaiSP'
					and macongsuat= '$MaCongSuat'
					and gia
					BETWEEN  '$GiaMin'
					AND  '$GiaMax'
						ORDER BY (ngaydang) DESC
						LIMIT $start, $display";
			$this->setQuery($sql);
			$arr = $this->loadAllRow();
			$this->disconnect();
			return $arr;	
		}
	}
?>